/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

function openDialog()
{
	var thisObject = this;
	
	this.options = new Array;

	this.height = 350;
	this.width = 550;
	this.selected = null;
	this.container = document.createElement('div');
	this.container.id = 'container';
	this.container.className = 'openDialogContainer';

	var scrollBarWidth = 40;
	
	document.getElementById('main').appendChild(this.container);
	
	$("#" + this.container.id).dialog({
			title: 'Kiwi - Open Document',
			height: this.height,
			width: this.width,
			modal: true,
			resizable: false
	});

	this.optionsList = document.createElement('div');
	this.optionsList.className = 'optionsList';
	this.container.appendChild(this.optionsList);
	
	this.preview = document.createElement('div');
	this.preview.className = 'preview';
	this.container.appendChild(this.preview);
	
	this.bbar = document.createElement('div');
	this.bbar.className = 'openDialogBBar';
	
	this.openButton = document.createElement('button');
	this.openButton.className = 'openDialogButton';
	this.openButton.innerHTML = "Open";
	this.openButton.onclick = function(){
		thisObject.close();
	};
	
	this.bbar.appendChild(this.openButton);
	this.container.appendChild(this.bbar);
	
	this.addOption = function(id, name){
		var option = document.createElement('li');
		option.className = 'openDialogOption';
		option.innerHTML =  name;
		this.optionsList.appendChild(option);
		this.options[id] = option;
	}
	
	this.setInfo = function(optionId, info){
		var option = this.options[optionId];
		
		option.onclick = function(){
			thisObject.showInfo(info);
			thisObject.selected = optionId;
		}
	}
	
	this.showInfo = function(info){
		this.preview.innerHTML = info;
	}
	
	this.open = function(){
		$("#" + this.container.id).dialog('open');
	}
	
	this.close = function(){
		$("#" + this.container.id).dialog('close');
		this.onClose(this.selected);
	}
	
	this.clear = function(){
		this.selected = null;
		this.options = [];
		this.optionsList.innerHTML = '';
		this.preview.innerHTML=''; 
	}
	
	this.onClose = function(selected){};
}